//SmoothFollow2D - Platformer Script
//Jason Keefer, 10/31/12
//David Lo, Edited: 11/23/12
@script RequireComponent (typeof(Camera))

//private var ortho: Matrix4x4;
//private var perspective: Matrix4x4;

//public var fov: float   = 60f;
//public var near: float  = .3f;
//public var far: float   = 1000f;
//public var orthographicSize: float = 100f;

//private var aspect: float;
//private var orthoOn: boolean;

var dampTime : float = 0.1; //offset from the viewport center to fix damping
private var velocity = Vector3.zero;
private var origination : Vector3; //variable to store origination point for relevant calculation
var target : Transform;

function Update() {
    if(target) {
        var point : Vector3 = camera.WorldToViewportPoint(target.position);
        var delta : Vector3 = target.position - camera.ViewportToWorldPoint(Vector3(0.5, 0.5, point.z));
        var destination : Vector3 = transform.position + delta;
        var change = 0.0;

		// Set this to the Y position you want the camera locked to
		// Make camera fade to no offset as player gets higher on Y axis
        if(target.position.y < (origination.y - 0.3)){
        	destination.y = 1.0;
        }
        else if(target.position.y < (origination.y + 0.162)){
        	change = 0.55;
        	destination.y = target.position.y + 0.55;
        }
        else if(target.position.y < (origination.y + 0.312)){
        	change = 0.475;
        	destination.y = target.position.y + 0.475;
        }
        else if(target.position.y < (origination.y + 0.437)){
        	change = 0.4;
        	destination.y = target.position.y + 0.4;
        }
        else if(target.position.y < (origination.y + 0.537)){
        	change = 0.325;
        	destination.y = target.position.y + 0.325;
        }
        else if(target.position.y < (origination.y + 0.637)){
        	change = 0.225;
        	destination.y = target.position.y + 0.225;
        }
        else if(target.position.y < (origination.y + 0.737)){
        	change = 0.125;
        	destination.y = target.position.y + 0.125;
        }
        else {
        	change = 0.0;
        	destination.y = target.position.y;
        }
        
        //David Lo:
        //Alter these lines to enable camera panning left/right/up/down
        
        //process up/down look
		if (Input.GetAxis("CamY") > 0.0){
			destination.y = destination.y - 0.5;
		}
		if (Input.GetAxis("CamY") < 0.0){
			destination.y = destination.y + 0.5;
		}
		
		//process left/right look
		if (Input.GetAxis("CamX") < 0.0){
			destination.x = destination.x - 1.4;
		}
		if (Input.GetAxis("CamX") > 0.0){
			destination.x = destination.x + 1.4;
		}
//		if (Input.GetAxis("Pan") > 0.0 || Input.GetKeyDown (KeyCode.RightArrow)){
//			destination.x = destination.x - 1.4;
//		}
//		if (Input.GetAxis("Pan") < 0.0 || Input.GetKeyDown (KeyCode.LeftArrow)){
//			destination.x = destination.x + 1.4;
//		}
		
        transform.position = Vector3.SmoothDamp(transform.position, destination, velocity, dampTime);
    }
	//if (Input.GetButtonDown("Run") || Input.GetKeyDown (KeyCode.Slash)){
		//if (Camera.main.orthographic==true) {
		   //Camera.main.orthographic=false;
		//}
		//else {
		   //Camera.main.orthographic=true;
		//}

//		orthoOn = !orthoOn;
//		if (orthoOn){
//			//transform.position.z -= 2.0;
//			BlendToMatrix(ortho, 1f);
//		}
//		else{
//			BlendToMatrix(perspective, 1f);
//			//transform.position.z += 2.0;
//			//fov.
//		}
	//}
}
     
function Awake ()
{
    //aspect = (Screen.width+0.0) / (Screen.height+0.0);
 
    //perspective = camera.projectionMatrix;
 
    //ortho = Matrix4x4.Ortho(-orthographicSize * aspect, orthographicSize * aspect, -orthographicSize, orthographicSize, near, far); 
    //orthoOn = false;
	//PlayerPrefs.DeleteAll();
    origination = target.position;
} 
 
static function MatrixLerp (from: Matrix4x4, to: Matrix4x4, time: float) : Matrix4x4
{
    var ret: Matrix4x4 = new Matrix4x4();
    var i: int;
    for (i = 0; i < 16; i++)
        ret[i] = Mathf.Lerp(from[i], to[i], time);
    return ret;
}
 
private function LerpFromTo (src: Matrix4x4, dest: Matrix4x4, duration: float) : IEnumerator
{
    var startTime: float = Time.time;
    while (Time.time - startTime < duration)
    {
        camera.projectionMatrix = MatrixLerp(src, dest, (Time.time - startTime) / duration);
        yield;
    }
    camera.projectionMatrix = dest;
}
 
public function BlendToMatrix (targetMatrix: Matrix4x4, duration: float) : Coroutine
{
    StopAllCoroutines();
    return StartCoroutine(LerpFromTo(camera.projectionMatrix, targetMatrix, duration));
}